Page({
    data: {
      newsList: [],
      isLoading: false,
      maxBehotTime: 0
    },
  
    onLoad: function() {
      this.loadNews();
    },
  
    onPullDownRefresh: function() {
      this.setData({ maxBehotTime: 0, newsList: [] });
      this.loadNews();
    },
  
    onReachBottom: function() {
      this.loadNews();
    },
  
    loadNews: function() {
      const that = this;
      const url = this.data.maxBehotTime === 0
        ? 'https://m.toutiao.com/list/?tag=__all__&max_time=0&min_behot_time=0&ac=wap&count=20&format=json_raw&_signature=xCd6dAAAosFlTl3HKFj1D8Qnem&i=1720748755&as=A116262940DA193&cp=66906AD149330E1&aid=1698'
        : 'https://m.toutiao.com/list/?tag=__all__&max_time=1720748755&max_behot_time=1720748755&ac=wap&count=20&format=json_raw&_signature=xCd6dAAAosFlTl3HKFj1D8Qnem&i=1720748755&as=A116262940DA193&cp=66906AD149330E1&aid=1698';
      
      this.setData({ isLoading: true });
  
      wx.request({
        url: url,
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          if (res.data && res.data.data && Array.isArray(res.data.data)) {
            let newNewsList = res.data.data.map(item => {
              // 假设 item 中有 images 和 video_cover 字段
              return {
                ...item,
                publish_days: that.calculateDays(item.publish_time),
                // 如果有视频，则使用视频封面
                video_cover: item.video_detail_info && item.video_detail_info.detail_video_large_image ? item.video_detail_info.detail_video_large_image.url : null
              };
            });
            that.setData({
              newsList: that.data.newsList.concat(newNewsList),
              // 如果响应中没有 next 字段，则不更新 maxBehotTime
              maxBehotTime: res.data.has_more ? res.data.max_behot_time : that.data.maxBehotTime,
              isLoading: false
            });
          } else {
            console.error('Invalid response data:', res.data);
            that.setData({ isLoading: false });
            wx.showToast({
              title: '加载失败',
              icon: 'none'
            });
          }
        },
        fail: () => {
          that.setData({ isLoading: false });
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
        }
      });
    },
  
    calculateDays: function(publishTime) {
      const now = new Date().getTime();
      const publish = new Date(publishTime * 1000).getTime();
      const days = Math.floor((now - publish) / (24 * 3600 * 1000));
      return days;
    }
  });
  